var o = require;
var t = module;
cc.Class({
    extends: cc.Component,
    properties: {ruwordsRoot: cc.Node, pbRuWords: cc.Prefab},
    onDestroy: function onDestroy() {
        this.winTimeout && clearTimeout(this.winTimeout);
    },
    start: function start() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this),
            this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this),
            this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this),
            this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this),
            (this.level = lGlobal.curLevel),
            (this.spacingXY = 5),
            (this.ruwordSize = 80),
            (this.ruwordsRootWidth = this.ruwordsRoot.width + this.spacingXY),
            (this.ruwordsRootHeight = this.ruwordsRoot.height + this.spacingXY),
            (this.ruwordses = []),
            (this.wordMap = []),
            (this.touchStartPos = null),
            (this.touchNowPos = null),
            (this.touchEndPos = null),
            (this.nowTouchedRuWords = null),
            (this.nowTouchedRuWordsPos = null);
        var o = cc.instantiate(this.pbRuWords);
        o.parent = this.ruwordsRoot;
        var t = o.getComponent("RuWords");
        t.rwMain = this;
        var s = lGlobal.lvCfg.json[lGlobal.curModel][lGlobal.curLevel],
            r = s.defMapWord;
        (r = JSON.parse(JSON.stringify(r))),
            t.setContent(r, !1),
            t.setMaskColor(lGlobal.getNextWordBgColor()),
            this.ruwordses.push(t),
            (this.wordMap = r),
            (this.blockScale = 0.5),
            (this.answers = s.answers),
            (this.answers = JSON.parse(JSON.stringify(this.answers)));
        for (var i = s.blocks, e = 0; e < i.length; e++) {
            var n = cc.instantiate(this.pbRuWords);
            (n.parent = this.ruwordsRoot),
                (n.scale = this.blockScale),
                (n.x = 160 * Math.floor(e % 5) - this.node.width / 2 + 50),
                (n.y = 120 * -Math.floor(e / 5) - this.node.height / 2 + 170);
            var h = n.getComponent("RuWords");
            (h.rwMain = this), h.setContent(i[e]), h.setMaskColor(lGlobal.getNextWordBgColor()), this.ruwordses.push(h);
        }
    },
    onTouchStart: function onTouchStart(o) {
        var t = o.getLocation();
        (this.touchStartPos = t), (this.touchNowPos = t);
    },
    onTouchMove: function onTouchMove(o) {
        var t = o.getLocation();
        (this.touchNowPos = t),
            this.nowTouchedRuWords &&
                this.nowTouchedRuWords.canMove &&
                (this.nowTouchedRuWords.node.position = this.nowTouchedRuWordsPos.add(
                    this.touchNowPos.sub(this.touchStartPos).mulSelf(1.3)
                ));
    },
    onTouchEnd: function onTouchEnd(o) {
        var t = this,
            s = o.getLocation();
        if (
            ((this.touchEndPos = s),
            (this.touchStartPos = null),
            (this.touchNowPos = null),
            this.nowTouchedRuWords && this.nowTouchedRuWords.canMove)
        ) {
            this.nowTouchedRuWordsPos = this.nowTouchedRuWords.node.position;
            var r = this.checkCanDrop(this.nowTouchedRuWords);
            if (r) {
                this.updateWordMap(this.nowTouchedRuWords, r);
                var i = this.getNowMakedWord(this.nowTouchedRuWords, r),
                    e = this.checkCanWord(i.istr);
                if (e.length > 0) {
                    this.nowTouchedRuWords.canMove = !1;
                    for (var n = 0; n < i.words.length; n++) {
                        i.words[n].canMove = !1;
                    }
                }
                for (var h = 0; h < e.length; h++) {
                    var d = this.answers.indexOf(e[h]);
                    d >= 0 && (this.answers.splice(d, 1), this.wordMapWordedAni(i.pos, h));
                }
                0 == this.answers.length &&
                    (this.winTimeout = setTimeout(function () {
                        window.ui.win({model: "RussiaWord", lv: t.level});
                    }, 1500));
                var u = this.nowTouchedRuWords.ruwords[0],
                    c = u.node.convertToWorldSpaceAR(cc.Vec2.ZERO),
                    a = this.ruwordsRoot.convertToNodeSpaceAR(c),
                    l = this.getPixelPosByLogicPos(
                        this.ruwordsRoot,
                        this.ruwordsRootWidth,
                        this.ruwordsRootHeight,
                        this.ruwordSize + this.spacingXY,
                        this.ruwordSize + this.spacingXY,
                        u.logicPos
                    ).sub(a);
                (l = l.add(cc.v2(this.ruwordSize / 2, -this.ruwordSize / 2))),
                    (this.nowTouchedRuWords.node.position = this.nowTouchedRuWords.node.position.add(l));
            } else
                (this.nowTouchedRuWords.node.position = this.nowTouchedRuWords.defPos),
                    (this.nowTouchedRuWords.node.scale = this.blockScale),
                    soundMgr.playSound(soundMgr.wrong);
            (this.nowTouchedRuWords.node.zIndex = 0), (this.nowTouchedRuWords = null);
        }
    },
    onTouchCancel: function onTouchCancel(o) {
        this.onTouchEnd(o);
    },
    ruWordsTouchStart: function ruWordsTouchStart(o) {
        (this.nowTouchedRuWords = o),
            (this.nowTouchedRuWords.node.scale = 1),
            (this.nowTouchedRuWords.node.zIndex = 10),
            (this.nowTouchedRuWordsPos = this.nowTouchedRuWords.node.position);
        for (
            var t = this.nowTouchedRuWords.getLogicPoses(
                    this.ruwordsRoot,
                    this.ruwordsRootWidth,
                    this.ruwordsRootHeight,
                    this.ruwordSize + this.spacingXY,
                    this.ruwordSize + this.spacingXY
                ),
                s = 0;
            s < t.length;
            s++
        ) {
            var r = !0,
                i = t[s].y,
                e = t[s].x;
            if (i >= this.wordMap.length || e >= this.wordMap[i].length || i < 0 || e < 0) r = !1;
            else {
                var n = this.wordMap[i][e];
                r = !(!n || "//" == n);
            }
            r && (this.wordMap[i][e] = "//");
        }
        soundMgr.playSound(soundMgr.selected);
    },
    checkCanDrop: function checkCanDrop(o) {
        for (
            var t = o.getLogicPoses(
                    this.ruwordsRoot,
                    this.ruwordsRootWidth,
                    this.ruwordsRootHeight,
                    this.ruwordSize + this.spacingXY,
                    this.ruwordSize + this.spacingXY
                ),
                s = t,
                r = 0;
            r < t.length;
            r++
        ) {
            var i = t[r].y,
                e = t[r].x;
            if (i < 0 || e < 0) s = null;
            else {
                if (i >= this.wordMap.length || e >= this.wordMap[i].length) {
                    s = null;
                    break;
                }
                var n = this.wordMap[i][e];
                if (n && "//" != n) {
                    s = null;
                    break;
                }
            }
        }
        return s;
    },
    getPixelPosByLogicPos: function getPixelPosByLogicPos(o, t, s, r, i, e) {
        var n = 0 - t * o.anchorX,
            h = s - s * o.anchorY,
            d = e.x * r + n,
            u = h - e.y * i;
        return cc.v2(d, u);
    },
    updateWordMap: function updateWordMap(o, t) {
        if (o)
            for (var s = 0; s < o.ruwords.length; s++) {
                var r = o.ruwords[s].label.string,
                    i = t[s];
                this.wordMap[i.y][i.x] = r;
            }
    },
    getNowMakedWord: function getNowMakedWord(o, t) {
        for (var s = {istr: [], pos: [], words: []}, r = 0; r < t.length; r++) {
            for (var i = t[r], e = this.wordMap[i.y][i.x], n = 0, h = i.x - 1; h > -1; h--) {
                var d = this.wordMap[i.y][h];
                if (!d || "//" == d) break;
                (e = d + e), n++;
            }
            for (var u = i.x + 1; u < this.wordMap[i.y].length; u++) {
                var c = this.wordMap[i.y][u];
                if (!c || "//" == c) break;
                e += c;
            }
            var a = this.checkSingleWord(e);
            if (a && a.x >= 0 && s.istr.indexOf(e) < 0) {
                s.istr.push(e);
                var l = i.x + a.x - n,
                    w = a.y;
                s.pos.push(cc.v4(l, i.y, 1, w));
                for (var g = 0; g < w; g++) {
                    var p = this.getWordByLogicPos(cc.v2(l + g, i.y));
                    if (p && p.length > 0) {
                        var f = p[0].node.parent.getComponent("RuWords");
                        f && f.canMove && s.words.indexOf(f) < 0 && s.words.push(f);
                    }
                }
            }
        }
        return s;
    },
    checkSingleWord: function checkSingleWord(o) {
        for (var t = null, s = 0; s < this.answers.length; s++) {
            var r = o.indexOf(this.answers[s]);
            if (r >= 0) {
                t = cc.v2(r, this.answers[s].length);
                break;
            }
        }
        return t;
    },
    checkCanWord: function checkCanWord(o) {
        for (var t = [], s = 0; s < o.length; s++) {
            for (var r = o[s], i = 0; i < this.answers.length; i++) {
                r.indexOf(this.answers[i]) >= 0 && t.indexOf(this.answers[i]) < 0 && t.push(this.answers[i]);
            }
        }
        return t;
    },
    wordMapWordedAni: function wordMapWordedAni(o, t) {
        for (var s = [], r = o[t], i = 0; i < r.w; i++) {
            var e = cc.v2(r.x + i, r.y),
                n = this.getWordByLogicPos(e);
            n.length >= 0 && (s = s.concat(n));
        }
        for (var h = lGlobal.getNextWordBgColor(), d = 0; d < s.length; d++) {
            s[d].setMaskColor(h);
        }
        soundMgr.playSound(soundMgr.right);
    },
    getWordByLogicPos: function getWordByLogicPos(o) {
        for (var t = [], s = 0; s < this.ruwordses.length; s++) {
            for (
                var r = this.ruwordses[s],
                    i = r.getLogicPoses(
                        this.ruwordsRoot,
                        this.ruwordsRootWidth,
                        this.ruwordsRootHeight,
                        this.ruwordSize + this.spacingXY,
                        this.ruwordSize + this.spacingXY
                    ),
                    e = 0;
                e < i.length;
                e++
            ) {
                var n = i[e];
                o.x == n.x && o.y == n.y && t.push(r.ruwords[e]);
            }
        }
        return t;
    },
    update: function update() {
        this.nowTouchedRuWords;
    }
});
